import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/equipment_inspection_ledger_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';

///设备检验检测台账
class EquipmentInspectionTestingLedgerController extends GetxController {
  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 搜索字符串
  String? searchX = '';

  /// 筛选条件列表
  List<FilterBean> filterList = [];

  /// 状态筛选条件
  String? status = '';

  /// 是否合格
  String? qualifiedNot = '';

  /// 选择的项目开始时间
  String? projectBeginDate = '';

  /// 选择的项目结束时间
  String? projectEndDate = '';

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    // 状态筛选条件
    final List<FilterEntitiesBean> statusList = [];
    statusList.add(FilterEntitiesBean(name: '待发起', value: '1'));
    statusList.add(FilterEntitiesBean(name: '待审批', value: '2'));
    statusList.add(FilterEntitiesBean(name: '审批中', value: '3'));
    statusList.add(FilterEntitiesBean(name: '通过', value: '4'));
    statusList.add(FilterEntitiesBean(name: '驳回', value: '5'));
    //是否合格
    final List<FilterEntitiesBean> qualifiedNot = [];
    qualifiedNot.add(FilterEntitiesBean(name: '合格', value: '1'));
    qualifiedNot.add(FilterEntitiesBean(name: '不合格', value: '0'));

    // 添加时间类型
    final List<FilterEntitiesBean> timeList = [];
    timeList.add(FilterEntitiesBean(name: '开始时间', value: projectBeginDate));
    timeList.add(FilterEntitiesBean(name: '结束时间', value: projectEndDate));

    //筛选控制bean
    final FilterBean filterBeanStatus = FilterBean(title: '状态', filterList: statusList);
    final FilterBean filterBeanQualified = FilterBean(title: '是否合格', filterList: qualifiedNot);
    final FilterBean filterBeanTime = FilterBean(title: '检验时间', filterList: timeList);
    filterList.add(filterBeanStatus);
    filterList.add(filterBeanQualified);
    filterList.add(filterBeanTime);
  }

  // 获取数据
  Future<List<EquipmentInspectionLedgerEntity>> equipmentInspectionLedgerList(int index, int size, {String? search}) async {
    final Map<String, dynamic> queryParameters = {
      'pageNum': index,
      'pageSize': size,
      'equipmentName': searchX, //设备名称
      'auditStatus': status, //状态 1:待发起 2:待审批3：审批中 4：通过 5：驳回
      'inspectionStatus': qualifiedNot, //检验结果：0，不合格，1.合格
    };
    if (projectBeginDate!.isNotEmpty) {
      queryParameters['startCheckTime'] = projectBeginDate;
    }
    if (projectEndDate!.isNotEmpty) {
      queryParameters['endCheckTime'] = projectEndDate;
    }
    final res = await HttpUtil()
        .generateGetRequest<List<EquipmentInspectionLedgerEntity>>(
          Interface.equipmentInspectionLedgerList,
          queryParameters: queryParameters,
        )
        .execute();
    List<EquipmentInspectionLedgerEntity>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
    });
    return list!;
  }

  /// 筛选
  void screen() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 筛选确认
  void screenConfirm(List<FilterBean> value) {
    filterList = value;
    status = '';
    projectBeginDate = '';
    projectEndDate = '';
    qualifiedNot = '';
    for (var i in value) {
      if (i.title == '检验时间') {
        for (var item in i.filterList) {
          switch (item.name) {
            case '开始时间':
              if (item.value != null && item.value != '') {
                projectBeginDate = item.value as String;
              }
              break;
            case '结束时间':
              if (item.value != null && item.value != '') {
                projectEndDate = item.value as String;
              }
              break;
          }
        }
      }
      if (i.chosenValue.isEmpty) {
        continue;
      }
      if (i.title == '状态') {
        status = i.chosenValue[0].value as String;
      }
      if (i.title == '是否合格') {
        qualifiedNot = i.chosenValue[0].value as String;
      }
    }
    update();
    refreshPage();
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}
